home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / PaletteAnimation / PaletteAnimation.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  4.7 KB  |  178 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                       Palette Demo                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*              Developer Technical Support             */
  6. /*                 Apple Computer, Inc.                 */
  7. /*                       08/15/90                       */
  8. /*______________________________________________________*/
  9.  
  10. #include    <Quickdraw.h>
  11. #include    <Windows.h>
  12. #include    <Events.h>
  13. #include     <GestaltEqu.h>
  14. #include    <OSEvents.h>
  15. #include    <Palettes.h>
  16. #include    <SegLoad.h>
  17. #include    <TextUtils.h>
  18.  
  19. extern _DataInit();
  20.  
  21. #define    TRUE            0xFF
  22. #define    FALSE            0
  23.  
  24. #define    clutID            150
  25. #define    numcolor        256
  26.  
  27. Rect                WinMinusScroll;
  28. WindowPtr            myWindow;
  29. CTabHandle            mycolors;
  30. PaletteHandle        srcPalette;
  31. Boolean                DoneFlag;
  32.  
  33. #ifdef powerc
  34.    QDGlobals    qd;
  35. #endif
  36.  
  37. /*______________________________________________________*/
  38. /*             Set Up Usage of Palette                  */
  39. /*______________________________________________________*/
  40. void Draw()
  41. /* The drawing routine is a collection of 253 random Rects which are colored with 
  42. the corresponding color in the color table. */
  43. {
  44.     float                    left, top, right, bottom;
  45.     Rect                    CRect;
  46.     int                        i;
  47.  
  48.     EraseRect (&WinMinusScroll);
  49.     for (i = 1; i < numcolor-2; ++i) {
  50.         do {
  51.             left = Random() / 32767.0 * WinMinusScroll.right;
  52.             if (left < 0)
  53.                 left = -left;
  54.             } while (left > WinMinusScroll.right - 40);
  55.             
  56.         do {
  57.             right = Random() / 32767.0 * WinMinusScroll.right;
  58.             if (right < 0)
  59.                 right = -right;
  60.             } while (right < left);
  61.             
  62.         do {
  63.             top = Random() / 32767.0 * WinMinusScroll.bottom;
  64.             if (top < 0)
  65.                 top = -top;
  66.             } while (top > WinMinusScroll.bottom - 40);
  67.             
  68.         do {
  69.             bottom = Random() / 32767.0 * WinMinusScroll.bottom;
  70.             if (bottom < 0)
  71.                 bottom = -bottom;
  72.             } while (bottom < top);
  73.         SetRect(&CRect, left, top, right, bottom);
  74.         PmForeColor(i);
  75.         FrameRect (&CRect);
  76.         PaintRect (&CRect);
  77.         }
  78. }
  79.  
  80. /*______________________________________________________*/
  81. /*               Initialization traps                   */
  82. /*______________________________________________________*/
  83. void init()
  84. {
  85.     Rect                BaseRect;
  86.     OSErr                err;
  87.     long                feature;
  88.  
  89.     UnloadSeg(_DataInit);
  90.     InitGraf(&qd.thePort);
  91.     FlushEvents(everyEvent, 0);
  92.     InitWindows();
  93.     InitCursor();
  94.     
  95.     DoneFlag = FALSE;
  96. /*______________________________________________________*/
  97. /*            Use Gestalt to find 32BQD                 */
  98. /*______________________________________________________*/
  99.     err = Gestalt(gestaltQuickdrawVersion, &feature);
  100.     if (!err) {
  101.         if ((feature & 0x0f00) != 0x0200)
  102.             DoneFlag = TRUE;
  103.         }
  104.     else 
  105.         DoneFlag = TRUE;
  106. /*______________________________________________________*/
  107. /*                     Set Rects                        */
  108. /*______________________________________________________*/
  109.     SetRect(&BaseRect, 40, 60, 472, 282);
  110.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-60, 
  111.                 BaseRect.bottom - 80);
  112. /*______________________________________________________*/
  113. /*        Open Window & set Palette & Picture           */
  114. /*______________________________________________________*/
  115.     mycolors = GetCTable (clutID);
  116.  
  117.     myWindow = NewCWindow(nil, &BaseRect, "\pUsing Palette Manager", TRUE, documentProc, 
  118.                             (WindowPtr) -1, TRUE, 150);
  119.     SetPort((WindowPtr) myWindow);
  120.  
  121.     srcPalette = NewPalette (numcolor, mycolors, pmAnimated, 0x1500);
  122.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  123.     
  124.     Draw();
  125. }
  126.  
  127. main()
  128. {
  129.     EventRecord     myEvent;
  130.     int                yieldTime = 0, Counter;
  131.     RGBColor        changecolor, black = {0,0,0};
  132.     long            startTicks, finalTicks;
  133.     char            *countsstrPtr, countstr;
  134. /*______________________________________________________*/
  135. /*                   Main Event loop                    */
  136. /*______________________________________________________*/
  137.     init();
  138.     countsstrPtr = &countstr;    
  139.     startTicks = TickCount();
  140.     Counter = 0;
  141.     for ( ;; ) {
  142.         if (DoneFlag) {
  143.             DoneFlag = FALSE;
  144.             RGBForeColor (&black);
  145.             NumToString(finalTicks,countsstrPtr);
  146.             MoveTo (20,20);
  147.             DrawString ("\pThe Number of Tick = ");
  148.             MoveTo (150,20);
  149.             DrawString (countsstrPtr);
  150.             for (;;) {
  151.                 if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  152.                     switch (myEvent.what) {
  153.                         case mouseDown:
  154.                         case keyDown:
  155.                         case autoKey:
  156.                             DoneFlag = TRUE;
  157.                             break;
  158.                         default:
  159.                             break;
  160.                         }
  161.                     }
  162.                 if (DoneFlag)
  163.                     ExitToShell();
  164.                 }
  165.             }
  166.             
  167.         /* Animate the colors one step for eash event loop */
  168.         GetEntryColor (srcPalette, 1, &changecolor);
  169.         AnimatePalette (myWindow, mycolors, 2, 1, numcolor - 2);
  170.         AnimateEntry (myWindow, numcolor - 1, &changecolor);
  171.         Palette2CTab (srcPalette, mycolors);
  172.         
  173.         if (++Counter == 1000) {
  174.             finalTicks = TickCount() - startTicks;
  175.             DoneFlag = TRUE;
  176.             }
  177.         }
  178. }